<?php

class Sistema_MaterialController extends App_Controller_Action
{

    private $_table     = 'MATERIAL';
    private $_pk        = 'MAT_ID';
    private $_order_col = 'MAT_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'MAT_STA';
    private $obj        = '';
    private $form       = '';
    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Material();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Material();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $where = $this->_pk . ' > 0';
        
        $for_id = $this->_getParam('for_id', 0);
        if($for_id == 0){
            $urlOptions = array('controller'=>'fornecedor','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        if($for_id > 0) $where .= ' and for_id = '.$for_id; 
        $this->view->for_id = $for_id;
                
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
        
        $this->view->voltar =  $this->_urlbase . '/' . $this->_modulo . '/fornecedor/';
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
		
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_material.js');
		
        $for_id = $this->_getParam('for_id', 0);
        if($for_id == 0){
            $urlOptions = array('controller'=>'fornecedor','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $this->form->getValues();
                    $data['FOR_ID'] = $for_id;
                    
                    unset($data['SEG_IDS']);

                    if($id == 0){
                        $this->obj->insert($data);
                        $id =  $this->obj->getAdapter()->lastInsertId(); 

                        $fsc = new Sistema_Model_DbTable_MaterialHasCat();
                        foreach($formData['SEG_CAT'] as $key => $arr_seg){
                            foreach($arr_seg as $seg){
                                $data_arr = array(
                                    'MAT_ID' => $id,
                                    //'CTS_ID' => $key,
                                    'CAT_ID' => $seg,
                                );
                                $fsc->insert($data_arr);
                            }
                        }
                    }
                    else{
                        $this->obj->update($data, $this->_pk.' = ' . $id);

                        $fsc = new Sistema_Model_DbTable_MaterialHasCat();
                        $fsc->delete('MAT_ID = '.$id);
                        foreach($formData['SEG_CAT'] as $key => $arr_seg){
                            foreach($arr_seg as $seg){
                                $data_arr = array(
                                    'MAT_ID' => $id,
                                    //'CTS_ID' => $key,
                                    'CAT_ID' => $seg,
                                );
                                $fsc->insert($data_arr);
                            }
                        }                        
                        
                    }
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','for_id'=>$for_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    //$this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.Errordb::descricaoError($ex->getCode()).'");</script>';
                echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $for_id = $this->_getParam('for_id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $this->obj->delete($this->_pk . ' = ' . $id);
            $fsc = new Sistema_Model_DbTable_MaterialHasCat();
            $fsc->delete($this->_pk . ' = ' . $id);
            $cor = new Sistema_Model_DbTable_Cor();
            
            $gradecor = new Sistema_Model_DbTable_GradecorHasCor();
            
            $cores = $cor->fetchAll('MAT_ID = ' . $id);
            foreach($cores as $cor) {
                $gradecor->delete('COR_ID ='.$cor->COR_ID);
            }
            
            $cor->delete($this->_pk . ' = ' . $id);
        }       
        //echo json_encode(array('ok' => $ok));
        $urlOptions = array('controller'=>$this->_controller,'action'=>'index','for_id'=>$for_id);
        $this->_helper->redirector->gotoRoute($urlOptions);
    }

}